﻿//Copyright (c) 2010 Christopher E. S. King (http://chriskingconsulting.com)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
// (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Reflection;
using System.Collections;
using System.IO;
using System.Diagnostics.Contracts;

using King.Extensions;
using King.Text;
using King.Reflection;
using King.Language;
using King.Language.CSharp.Antlr3;

using Antlr.Runtime;
using Antlr.Runtime.Tree;

namespace King.Language.CSharp.Antlr3 {

    public partial class CSharpAst {
        public override void Recover(IIntStream input, RecognitionException re) {
            throw re;
        }
    }
    public partial class CSharpParser {

        private HashSet<string> m_ppSymbols;

        partial void OnCreated() {
            m_ppSymbols = new HashSet<string>(System.StringComparer.InvariantCultureIgnoreCase);
        }
        private void Define(IToken token) {
            m_ppSymbols.Add(token.Text);
        }
        private void Undefine(IToken token) {
            m_ppSymbols.Remove(token.Text);
        }
        private bool IsDefined(IToken token) {
            return m_ppSymbols.Contains(token.Text);
        }
        private bool ParseBool(IToken token) {
            return bool.Parse(token.Text);
        }

        protected override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow) {
            throw new MismatchedTokenException(ttype, input, TokenNames);
        }
        public override void Recover(IIntStream input, RecognitionException re) {
            throw re;
        }

        private CSharpLexer Lexer { get { return (CSharpLexer)TokenStream.TokenSource; } }
        private bool SkipSection {
            get { return Lexer.SkipSection; }
            set { Lexer.SkipSection = value; }
        }

        internal bool EmitPreProcessor { get; set; }
        internal bool EmitComments { get; set; }
        internal void AddPreProcessorSymbols(IEnumerable<string> symbols) {
            if (symbols == null)
                return;
            foreach (var symbol in symbols) m_ppSymbols.Add(symbol);
        }

        private int m_indent;
        partial void EnterRule(string ruleName, int ruleIndex) {
            Console.WriteLine("".PadLeft(m_indent++) + "Enter " + ruleName + " " + ruleIndex);
        }
        partial void LeaveRule(string ruleName, int ruleIndex) {
            Console.WriteLine("".PadLeft(--m_indent) + "Leave " + ruleName + " " + ruleIndex);
        }
    }
    public partial class CSharpLexer {

        private bool m_skipSection;
        partial void OnSkipSection(bool value);
        internal bool SkipSection {
            get { return m_skipSection; }
            set {
                OnSkipSection(value);
                m_skipSection = value;
            }
        }

        protected override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow) {
            throw new MismatchedTokenException(ttype, input, TokenNames);
        }

        partial void OnSkipSection(bool value) {
            Console.WriteLine("SkipSection = " + value);
        }
        public override IToken Emit() {
            var token = base.Emit();
            Console.WriteLine(token.ToString());
            return token;
        }
    }
}
